package avicit.bdp.dgs.metadata.rest;

import avicit.bdp.dgs.metadata.dto.MetadataModelDTO;
import avicit.bdp.dgs.metadata.service.MetadataModelService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mayi 元模型控制层
 */
@RestController
@Api(tags = "元模型")
@RequestMapping("/api/bdp/dgs/metadata/model")
public class MetadataModelRest {

  @Resource private MetadataModelService metadataModelService;

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "试用数据源类型",
        name = "applyDatasourceType",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<MetadataModelDTO>> getPageList(
      @RequestParam(required = false) String applyDatasourceType,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<MetadataModelDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<MetadataModelDTO> queryRespBean =
        metadataModelService.getPageList(applyDatasourceType, keyWords, pageNo, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "新增")
  @PostMapping
  public ResponseMsg<String> save(@RequestBody MetadataModelDTO dto) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataModelService.save(dto));
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataModelService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<MetadataModelDTO> queryById(@PathVariable String id) {
    ResponseMsg<MetadataModelDTO> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataModelService.queryById(id));
    return responseMsg;
  }

  @ApiOperation(value = "状态变更")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "元模型ID", name = "id", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态值", name = "status", dataType = "Int", paramType = "query")
  })
  @PutMapping("/changeStatus")
  public ResponseMsg<Boolean> changeStatus(@RequestParam String id, @RequestParam Integer status) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataModelService.changeStatus(id, status));
    return responseMsg;
  }
}
